package com.dao.Impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.dao.BaseDao;
import com.dao.SpecialDao;
import com.entity.Special;
import com.entity.Data.PagerData;
import com.entity.Query.SpecialQuery;
import com.util.MyUtil;

@Repository
public class SpecialDaoImpl extends BaseDao implements SpecialDao {

	@Override
	public List<Map<String, Object>> querySpecial() {
		String sql ="select sp.specialId,sp.specialName,sp.specialType,sp.`language`,"
				+ "sp.issueDate,sp.recordCompany,sp.specialPhoto,sp.specialIntro,si.singerName "
				+ "from special as sp join singer as si on sp.singerId=si.singerId";
		return executeQuery(sql, null);
	}

	@Override
	public Map<String, Object> querySpecialId(int specialId) {
		String sql ="select sp.specialId,sp.specialName,sp.specialType,sp.`language`,"
				+ "sp.issueDate,sp.recordCompany,sp.specialPhoto,sp.specialIntro,si.singerName "
				+ "from special as sp join singer as si on sp.singerId=si.singerId where specialId=?";
		Object[] params={specialId};
		return executeQueryMap(sql, params);
	}

	@Override
	public int addSpecial(Special special) {
		String sql ="insert into special(specialName,specialType,"
				+ "language,issueDate,recordCompany,specialPhoto,specialIntro,singerId)"
				+ "values(?,?,?,?,?,?,?,?)";
		Object[] params={special.getSpecialName(),special.getSpecialType(),
				special.getLanguage(),special.getIssueDate(),
				special.getRecordCompany(),special.getSpecialPhoto(),special.getSpecialIntro(),special.getSingerId()};
		return executeUpdate(sql, params);
	}

	@Override
	public int editSpecial(Special special) {
		String sql ="update special	set specialName=?,specialType=?,language=?,issueDate=?,"
				+ "recordCompany=?,specialIntro=?,singerId=? 	where specialId=?";
		Object[] params={special.getSpecialName(),special.getSpecialType(),special.getLanguage()
				,special.getIssueDate(),special.getRecordCompany(),special.getSpecialIntro(),special.getSingerId(),special.getSpecialId()};
		return executeUpdate(sql, params);
	}

	@Override
	public int delSpecial(int specialId) {
		String sql ="delete from special where specialId=?";
		Object[] params={specialId};
		return executeUpdate(sql, params);
	}

	@Override
	public PagerData queryByPage(SpecialQuery specialQuery, int curPage, int pageSize) {
		//根据条件拼接where语句
		String sqlWhere = "";
		List<Object> params = new ArrayList<Object>();
		//专辑名字条件不为空
		if(!MyUtil.isNull(specialQuery.getSpecialName())){
			sqlWhere +=" and sp.specialName like ? ";
			params.add(specialQuery.getSpecialName());
		}
		//流派条件不为空
		if(!MyUtil.isNull(specialQuery.getSpecialType())){
			sqlWhere +=" and sp.specialType like ? ";
			params.add(specialQuery.getSpecialType());
		}
		//语种条件不为空
		if(!MyUtil.isNull(specialQuery.getLanguage())){
			sqlWhere +=" and sp.language like ? ";
			params.add(specialQuery.getLanguage());
		}
		//唱片公司条件不为空
		if(!MyUtil.isNull(specialQuery.getRecordCompany())){
			sqlWhere +=" and sp.recordCompany like ? ";
			params.add(specialQuery.getRecordCompany());
		}
		//歌手条件不为空
		if(specialQuery.getSingerId()!=null){
			sqlWhere += " and si.singerId = ? ";
			params.add(specialQuery.getSingerId());
		}
		//获取带条件查询的总记录数
		String sqlTotal = "select count(*) from special as sp "
				+ "join singer as si "
				+ "on sp.singerId=si.singerId where 1=1"+sqlWhere;
		int totalCount = jdbcTemplate.queryForInt(sqlTotal,params.toArray());
		//根据总记录数获取总页数
		int totalPage = totalCount%pageSize==0 ?
				        totalCount/pageSize    :
				        totalCount/pageSize+1;
		//查询当前页的数据记录
		String sql = "select sp.specialId,sp.specialName,sp.specialType,"
				+ "sp.`language`,sp.issueDate,sp.recordCompany,"
				+ "sp.specialPhoto,sp.specialIntro,si.singerId,si.singerName "
				+ "from special as sp join singer as si "
				+ "on sp.singerId=si.singerId where 1=1"+sqlWhere
				+ "   order by sp.specialId "
				+ "   limit ?,? ";
		//计算分页查询的开始位置
		int start = (curPage-1)*pageSize;
		
		params.add(start);
		params.add(pageSize);
		List<Map<String,Object>> dataList = executeQuery(sql,params.toArray());
		//封装分页数据对象
		PagerData pd = new PagerData();
		//当前第几页
		pd.setCurPage(curPage);
		//每页几条
		pd.setPageSize(pageSize);
		//总记录数
		pd.setTotalCount(totalCount);
		//总页数
		pd.setTotalPage(totalPage);
		//当前页数据记录
		pd.setDataList(dataList);
		return pd;
	}

}
